package adee.qrlogin;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */

    private Context context;
    private LoginManager loginManager;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context=this;

        this.loginManager = new LoginManager((Activity) context);
        ImageView iw = (ImageView) findViewById(R.id.key_hole_image);
        iw.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                loginManager.startLoginProcedure();
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        this.loginManager.onActivityResult(requestCode, resultCode, intent);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.main_menu, menu);
      return true;
   }

   private void showAccounts(){
       Intent   i = new Intent(context, Accounts.class);
       context.startActivity(i);
   }

   private void showHelp(){
       Intent   i = new Intent(context, Help.class);
       context.startActivity(i);
   }

   private void showAbout(){
       Intent   i = new Intent(context, About.class);
       context.startActivity(i);
   }

   private void logoutAll(){
        new AlertDialog.Builder(context)
            .setIcon(R.drawable.kerdojel)
            .setTitle(R.string.logout)
            .setMessage(R.string.really_logout)
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

               @Override
               public void onClick(DialogInterface dialog, int which) {
                    loginManager.logoutAll();
                }

             }).setNegativeButton(R.string.no, null)
             .show();
   }

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.logout_all:
             this.logoutAll();
        return true;
    case R.id.help:
             this.showHelp();
        return true;

   case R.id.about_menu:
             this.showAbout();
   return true;
   case R.id.login_menu:
            this.loginManager.startLoginProcedure();
       return true;

   case R.id.account_list:
             this.showAccounts();
             
        return true;

    case R.id.refresh_menu:
             this.loginManager.refreshAll();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

}
